package com.javaee.lpr.service.Impl;

import com.javaee.lpr.entity.Notice;
import com.javaee.lpr.entity.User;
import com.javaee.lpr.mapper.NoticeMapper;
import com.javaee.lpr.mapper.UserMapper;
import com.javaee.lpr.service.IUserService;
import com.javaee.lpr.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Override // 注册
    public void register(User user) {
        Integer row = 0;
        String username = user.getUsername();
        User resultUser = userMapper.findByUsername(username);
        // 用户名重复
        if(resultUser !=null){
            throw new NameDuplicatedException("用户名被占用");
        }
        // 信息补全
        user.setIdentity(0);
        // 时间信息
//        Date date = new Date();
//        user.set...(date);
        row = userMapper.insert(user);
        if(row != 1){
            throw new InsertException("注册时发生未知异常");
        }
    }

    // 登录
    @Override
    public User login(String username, String password) {
        User result = userMapper.findByUsername(username);
        if(result == null){
            throw new UserNotFoundException("用户数据不存在");
        }
        // 密码比较
        String resultPSW = result.getPassword();
        if(!resultPSW.equals(password)){
            throw new PasswordNotMatchException("密码不匹配");
        }
        return result;
    }

    @Override
    public void changePassword(Integer id, String username, String oldPassword, String newPassword) {
        User result = userMapper.findById(id);
        if(result==null){
            throw new UserNotFoundException("用户不存在");
        }
        // 密码不匹配
        if(!oldPassword.equals(result.getPassword())){
            throw new PasswordNotMatchException("密码错误");
        }
        // 更新
        Integer row = userMapper.updatePasswordById(id, newPassword);
        if(row != 1){
            throw new UpdateException("更改密码时发生未知错误");
        }
    }

    @Override
    public void addEmployee(User user) {
        Integer row = 0;
        String username = user.getUsername();
        User resultUser = userMapper.findByUsername(username);
        // 用户名重复
        if(resultUser !=null){
            throw new NameDuplicatedException("员工已添加");
        }
        // 信息补全 身份码为1
        user.setIdentity(1);
        // 时间信息
//        Date date = new Date();
//        user.set...(date);
        row = userMapper.insert(user);
        if(row != 1){
            throw new InsertException("添加员工时发生未知异常");
        }
    }

    @Override
    public void deleteEmployee(User user) {
        String username = user.getUsername();
        User result = userMapper.findByUsername(username);
        if(result == null){
            throw new UserNotFoundException("用户数据不存在");
        }
        // 密码比较
        String password = user.getPassword();
        String resultPSW = result.getPassword();
        if(!resultPSW.equals(password)){
            throw new PasswordNotMatchException("密码不匹配");
        }
        Integer row = userMapper.delete(user);
        if(row != 1){
            throw new InsertException("删除员工时发生未知异常");
        }
    }

    @Override
    public void addNotice(Notice notice) {
        Integer row = 0;

        // 先查询是否有重复title
        String title = notice.getTitle();
        Notice resultNotice = noticeMapper.findByTitle(title);
        // 通知名重复
        if(resultNotice !=null){
            throw new NameDuplicatedException("已有此通知");
        }
        // 补全时间信息
        Date date = new Date();
        notice.setDate(date);
        row = noticeMapper.insert(notice);
        if(row != 1){
            throw new InsertException("添加通知时发生未知异常");
        }
    }

}
